<%-- 
    Document   : page
    Created on : 2014/8/21, 上午 09:27:58
    Author     : gjun
--%>

<%@page import="java.util.HashMap"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%
    String username = request.getParameter("username");
    String where =" ";
    if( username != null && !"".equals(username)){
        where =" where username like '%" + username+"%' ";
    }
    //----------------------------------------
    dao.BaseDAO y= new dao.BaseDAO();
    String sql="select count(*) as total from person "+ where;//顯示資料總筆數 並命名(as)total
    String ary[]={};
    ArrayList list=y.query(sql, ary);
    //System.out.println(list);
    String total= (String)((HashMap)list.get(0)).get("total");//強制轉換成String型態
    
    int pagesize = 10;//一頁有幾筆
    int pages = Integer.parseInt(total)/pagesize;//Integer.parseInt(total)轉換total為int 並計算頁數
    if (Integer.parseInt(total)%pagesize != 0)//計算有幾頁 
        pages++;//有尾數不足加1
    String pageNo=request.getParameter("pageNo");//初始頁碼判斷
    if (pageNo==null)//若在第一頁 則=1
        pageNo="1";
    
    int index=(Integer.parseInt(pageNo)- 1) *pagesize;//資料庫中開始的位置
    
    sql="select * from person "+where+" limit "+index+","+pagesize;//(limit 開始位置,一頁顯示幾個)
    //where 要在limit前
    list = y.query(sql, ary);
    System.out.println(list);
    int lastPage=Integer.parseInt(pageNo)==1 ? 1 : Integer.parseInt(pageNo)-1;//上一頁
    int nextPage=Integer.parseInt(pageNo)==pages ?  pages :Integer.parseInt(pageNo)+1;//下一頁
    
    //String sql1="select id , username , tel ,birthday from person";
    request.setAttribute("list", list);//列出各頁內容 參考dao4.jsp
%>
<c:set var="list" value="<%= list %>"/>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>分頁切換</h1>
        
        <form action="page.jsp" id="form1"><!--加id 供javascript -->
            查詢條件:<input type="text" name="username" value="${param.username}">
            <!-- value="${param.username} 把搜尋字串留置提交後不消失-->
            <input type="hidden" name="pageNo" id="pageNo" value="1">
            <input type="submit">
        </form> 
            <input type="button" value="上一頁" onclick="goPage('<%= lastPage %>')">
        <!-- <a href ="page.jsp?pageNo=<%=lastPage %>">上一頁</a> -->
        總共有<%= total %>筆, 目前頁碼:<%=pageNo %>,
        每頁<%=pagesize %>筆, 共有幾頁:<%=pages %>頁
         <input type="button" value="下一頁" id='id' onclick="goPage('<%= nextPage %>')">
        <!-- <a href="page.jsp?pageNo=<%=nextPage %>">下一頁 --></a><br>
        <script>
            function goPage(z){//上面goPage('<%= nextPage %>') 呼叫goPage 並帶入<%= nextPage %>
               document.getElementById('pageNo').value= z;
               document.getElementById('form1').submit();
            }
         </script>
        ------------------------------------------------------------------------------<br>
        <c:forEach var="x" items="${list}"><!-- 參考dao4.jsp-->
            
            <c:out value="${x.username}"/>
            <c:out value="${x.tel}"/>
            <c:out value="${x.birthday}"/><br>
            <br>
        </c:forEach>
        
    </body>
</html>
